package mianyang.zhangsan.com.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import mianyang.zhangsan.com.mapper.RoomTypeMapper;
import mianyang.zhangsan.com.pojo.RoomType;
import mianyang.zhangsan.com.service.RoomTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.management.Query;
import java.util.List;

@Service("roomTypeService")
public class RoomTypeServiceImpl implements RoomTypeService {
    @Autowired
    private RoomTypeMapper roomTypeMapper;

    /**
     * 分页查询所有的房间类型信息
     * @param pageNo
     * @return
     */
    @Override
    public Page<RoomType> queryPages(int pageNo) {
        Page<RoomType> roomTypePage = roomTypeMapper.selectPage(new Page<>(pageNo, 3), null);
        return roomTypePage;
    }

    /**
     * 查询所有的房间类型信息
     * @return
     */
    @Override
    public List<RoomType> queryAll() {
        return roomTypeMapper.selectList(null);
    }

    /**
     * 根据id查询房间类型信息
     * @param roomType
     * @return
     */
    @Override
    public RoomType queryRoomTypeInfo(int roomType) {
        QueryWrapper<RoomType> wrapper = new QueryWrapper<>();
        wrapper.eq("roomtypeid", roomType);
        return roomTypeMapper.selectOne(wrapper);
    }

    /**
     * 添加房间类型
     * @param roomType
     */
    @Override
    public void addRoomType(RoomType roomType) {
        roomTypeMapper.insert(roomType);
    }

    /**
     * 根据房间类型查询
     * @param roomTypeName
     * @return
     */
    @Override
    public RoomType queryRoomTypeInfo(String roomTypeName) {
        QueryWrapper<RoomType> wrapper = new QueryWrapper<>();
        wrapper.eq("roomtypename", roomTypeName);
        return roomTypeMapper.selectOne(wrapper);
    }

    /**
     * 根据查询条件查询
     * @param page
     * @param rows
     * @param roomType
     * @return
     */
    @Override
    public Page<RoomType> queryPageByWrapper(Integer page, Integer rows, RoomType roomType) {
        QueryWrapper<RoomType> wrapper = queryWrapper(roomType);
        return roomTypeMapper.selectPage(new Page<>(page, rows), wrapper);
    }

    /**
     * 根据条件查询所有的房间类型
     * @param roomType
     * @return
     */
    @Override
    public List<RoomType> queryListByWrapper(RoomType roomType) {
        QueryWrapper<RoomType> wrapper = new QueryWrapper<>();
        return roomTypeMapper.selectList(wrapper);
    }

    /**
     * 封装查询条件
     * @param roomType
     * @return
     */
    @Override
    public QueryWrapper<RoomType> queryWrapper(RoomType roomType) {
        QueryWrapper<RoomType> wrapper = new QueryWrapper<>();
        if (roomType.getRoomTypeId() != null){
            wrapper.eq("roomtypeid", roomType.getRoomTypeId());
        }
        if (roomType.getRoomTypeName() != null && !"".equals(roomType.getRoomTypeName())) {
            wrapper.eq("roomtypename", roomType.getRoomTypeName());
        }
        if (roomType.getPrice() != null){
            wrapper.eq("price", roomType.getPrice());
        }
        return wrapper;
    }

    /**
     * 删除一条记录
     * @param roomTypeId
     */
    @Override
    public void delete(int roomTypeId) {
        QueryWrapper<RoomType> wrapper = new QueryWrapper<>();
        wrapper.eq("roomtypeid", roomTypeId);
        roomTypeMapper.delete(wrapper);
    }

    /**
     * 删除多条记录
     * @param roomTypeIds
     */
    @Override
    public void deletes(String roomTypeIds) {
        String[] roomTypes = roomTypeIds.split(",");
        for (String roomType:roomTypes){
            int id = Integer.parseInt(roomType);
            delete(id);
        }
    }

    /**
     * 更新房间类型信息
     * @param roomTypeId
     * @param roomType
     */
    @Override
    public void update(int roomTypeId, RoomType roomType) {
        QueryWrapper<RoomType> wrapper = new QueryWrapper<>();
        wrapper.eq("roomtypeid", roomTypeId);
        roomTypeMapper.update(roomType, wrapper);
    }
}
